package com.ruoyi.ylyx.controller;

import java.util.List;

import com.ruoyi.common.utils.PingYinUtil;
import com.ruoyi.ylyx.service.IYlyxGameService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ylyx.domain.YlyxGametype;
import com.ruoyi.ylyx.service.IYlyxGametypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 游戏分类Controller
 * 
 * @author yuexl
 * @date 2021-02-05
 */
@Controller
@RequestMapping("/ylyx/gametype")
public class YlyxGametypeController extends BaseController
{
    private String prefix = "ylyx/gametype";

    @Autowired
    private IYlyxGametypeService ylyxGametypeService;
    @Autowired
    private IYlyxGameService ylyxGameService;

    @RequiresPermissions("ylyx:gametype:view")
    @GetMapping()
    public String gametype()
    {
        return prefix + "/gametype";
    }

    /**
     * 查询游戏分类列表
     */
    @RequiresPermissions("ylyx:gametype:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YlyxGametype ylyxGametype)
    {
        startPage();
        List<YlyxGametype> list = ylyxGametypeService.selectYlyxGametypeList(ylyxGametype);
        return getDataTable(list);
    }

    /**
     * 导出游戏分类列表
     */
    @RequiresPermissions("ylyx:gametype:export")
    @Log(title = "游戏分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YlyxGametype ylyxGametype)
    {
        List<YlyxGametype> list = ylyxGametypeService.selectYlyxGametypeList(ylyxGametype);
        ExcelUtil<YlyxGametype> util = new ExcelUtil<YlyxGametype>(YlyxGametype.class);
        return util.exportExcel(list, "gametype");
    }

    /**
     * 新增游戏分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存游戏分类
     */
    @RequiresPermissions("ylyx:gametype:add")
    @Log(title = "游戏分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YlyxGametype ylyxGametype)
    {
        ylyxGametype.setTypeCode(PingYinUtil.getFirstSpell(ylyxGametype.getTypeName()));
        return toAjax(ylyxGametypeService.insertYlyxGametype(ylyxGametype));
    }

    /**
     * 修改游戏分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        YlyxGametype ylyxGametype = ylyxGametypeService.selectYlyxGametypeById(id);
        mmap.put("ylyxGametype", ylyxGametype);
        return prefix + "/edit";
    }

    /**
     * 修改保存游戏分类
     */
    @RequiresPermissions("ylyx:gametype:edit")
    @Log(title = "游戏分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YlyxGametype ylyxGametype)
    {
        ylyxGametype.setTypeCode(ylyxGametype.getTypeName());
        return toAjax(ylyxGametypeService.updateYlyxGametype(ylyxGametype));
    }

    /**
     * 删除游戏分类
     */
    @RequiresPermissions("ylyx:gametype:remove")
    @Log(title = "游戏分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //如果有游戏选择了此分类，则不能删除
        if(ylyxGameService.selectYlyxGameListByGameType(ids).size()>0){
            return error("不能删除，此分类下面关联的有游戏！");
        }
        return toAjax(ylyxGametypeService.deleteYlyxGametypeByIds(ids));
    }
}
